<template>
  <a-form ref="form" :rules="rules" :model="formData" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
    <a-form-item label="类型标识" name="bigType">
      <a-select v-model:value="formData.bigType" :options="bigTypeList" :placeholder="rules['bigType'][0].message" />
    </a-form-item>
    <a-form-item label="包含类别" name="people_type">
      <a-select
        v-model:value="formData.people_type"
        :options="smallTypeList[formData.bigType]"
        :placeholder="rules['people_type'][0].message"
      />
    </a-form-item>
    <a-form-item v-if="formData.people_type == 'D0016_6'" label="风险等级" name="risk_grade">
      <a-select v-model:value="formData.risk_grade" :options="fxdjList" :placeholder="rules.risk_grade[0].message" />
    </a-form-item>
    <a-form-item v-if="formData.people_type == 'D0016_6'" label="走访频率" name="visit_frequency">
      <a-select
        v-model:value="formData.visit_frequency"
        :options="[
          { label: '每月', value: 1 },
          { label: '三月', value: 2 },
          { label: '半年', value: 3 },
          { label: '一年', value: 4 },
          { label: '无需走访', value: 0 },
        ]"
        :placeholder="rules.visit_frequency[0].message"
      />
    </a-form-item>
    <a-form-item label="短信接收人">
      <a-select v-model:value="formData.zz_people_id" :options="linkmanList" placeholder="请选择短信接收人" />
    </a-form-item>
    <a-form-item label="人员代号" name="people_code">
      <a-input v-model:value="formData.people_code" :placeholder="rules['people_code'][0].message" />
    </a-form-item>
    <a-form-item label="标记说明">
      <a-textarea v-model:value="formData.reason" placeholder="请输入标记说明" />
    </a-form-item>
  </a-form>
</template>

<script>
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { useDicDataStore } from '@gis/lib/pinia/dicData.js';
import { handleCommonRes, handleFormRes } from '@gis/lib/utils/resolver.js';
import {
  peopleBusinessTagBigType,
  peopleBusinessTagSmallType,
  peopleBusinessTagAdd,
  teamPeopleSelect,
} from '@gis/lib/api';
import isEmpty from 'lodash-es/isEmpty.js';

export default {
  name: 'add-tag',
  components: {},
  props: { people_id: { type: String } },
  data() {
    return {
      gzryList: [], //选择的工作人员列表
      syrkList: [],
      formData: {},
      rules: {
        bigType: [{ required: true, message: '请选择类型标识' }],
        people_type: [{ required: true, message: '请选择类型' }],
        people_code: [{ required: true, message: '请输入人员代号' }],
        risk_grade: [{ required: true, message: '请选择风险等级' }],
        visit_frequency: [{ required: true, message: '请选择走访频率' }],
      },
      bigTypeList: [],
      smallTypeList: {}, //减少请求
      linkmanList: [], //接收消息
    };
  },
  watch: {
    'formData.bigType': {
      immediate: true,
      handler(val) {
        this.formData.people_type = undefined;
        if (val) {
          this.getSmallTypeList({ parent_id: val });
        }
      },
    },
  },
  computed: {
    fxdjList() {
      const useDic = useDicDataStore();
      return useDic.getDicDataList('D0090');
    },
  },
  async created() {
    this.getBigTypeList(); //大类请求
    this.getLinkPeopleList(); //短信接收人
  },
  methods: {
    async onOk() {
      const valid = await this.$refs.form.validate();
      if (!valid) return false;
      let params = Object.assign({}, this.formData, { people_id: this.people_id });
      delete params['bigType'];
      return await handleFormRes(peopleBusinessTagAdd(params));
    },
    async getSmallTypeList(params) {
      const { parent_id } = params;
      if (!isEmpty(this.smallTypeList[parent_id])) return false;
      let data = await handleCommonRes(peopleBusinessTagSmallType(params));
      if (data.length) {
        data = data.map((item) => Object.assign({}, item, { label: item.value, value: item.flag }));
      }
      this.smallTypeList[parent_id] = data;
    },
    async getLinkPeopleList() {
      const userData = useUserInfoStore();
      const data = await handleCommonRes(teamPeopleSelect({ unit_code: userData.unit_code }));
      this.linkmanList = data.map((item) => Object.assign({}, { label: item.name, value: item.id }));
    },
    async getBigTypeList() {
      const data = await handleCommonRes(peopleBusinessTagBigType());
      this.bigTypeList = data.length ? data.map((item) => Object.assign({}, { label: item.name, value: item.id })) : [];
    },
  },
};
</script>

<style scoped lang="less"></style>
